package com.peanut.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.common.Result;
import com.peanut.domain.bo.TabCarBrandBO;
import com.peanut.domain.po.TabCarBrandPO;
import com.peanut.dto.TabCarBrandDTO;
import com.peanut.service.TabCarBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("brand")
@Api("品牌管理Api")
public class BrandController {

    @Resource
    private TabCarBrandService brandService;

    /**
     * 添加品牌
     * @param dto : 品牌实体类中的数据
     * @return
     */
    @PostMapping
    @ApiOperation("添加品牌")
    @ApiImplicitParam(name = "dto",value = "品牌实体类",required = true,paramType ="body")
    public Result addBrand(@RequestBody TabCarBrandDTO dto){
        try {
            TabCarBrandBO bo = new TabCarBrandBO();
            BeanUtils.copyProperties(dto,bo);
            int i = brandService.addBrand(bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 查询所有品牌,并分页
     * @param pageNumber : 当前页面
     * @param pageSize : 每页条数
     * @return
     */
    @PostMapping("findAll")
    @ApiOperation("查询所有品牌")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber",value = "当前页面",required = true,paramType ="query"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,paramType ="query")
    })
    public Result findAll(@RequestParam(value = "pageNumber",defaultValue = "1")Integer pageNumber,
                          @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        try {
            Page<TabCarBrandPO> page = brandService.findAll(pageNumber,pageSize);
            return Result.OK().setData(page);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 修改品牌
     * @param dto : 品牌实体类中的数据
     * @return
     */
    @PutMapping
    @ApiOperation("修改品牌")
    @ApiImplicitParam(name = "dto",value = "品牌实体类",required = true,paramType ="body")
    public Result updateBrand(@RequestBody TabCarBrandDTO dto){
        try {
            TabCarBrandBO bo = new TabCarBrandBO();
            BeanUtils.copyProperties(dto,bo);
            int i = brandService.updateBrand(bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 根据ID删除品牌
     * @param carBrandId : 品牌ID
     * @return
     */
    @DeleteMapping("/{carBrandId}")
    @ApiOperation("根据ID删除品牌")
    @ApiImplicitParam(name = "infoId",value = "品牌ID",required = true,paramType ="path")
    public Result deleteBrand(@PathVariable Long carBrandId){
        try {
            int i = brandService.deleteBrand(carBrandId);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }
}
